import { TimeFilter } from "qqlx-core";

import { toNumber, toString } from "@lib/com.verify";

/** 今天0点～今天午夜12点 */
export function getRangeDay(): TimeFilter {
  const today = new Date();
  today.setHours(0);
  today.setMinutes(0);
  today.setSeconds(0);
  today.setMilliseconds(0);

  const time = today.getTime();
  return {
    startTime: toString(time),
    endTime: toString(time + 86400000 - 1),
  };
}

/** 本周一0点～现在 */
export function getRangeWeek(): TimeFilter {
  const today = new Date();
  today.setHours(0);
  today.setMinutes(0);
  today.setSeconds(0);
  today.setMilliseconds(0);

  const day = today.getDay(); // 今天是星期几, 星期天是0
  const monday = today.getTime() - ((day === 0 ? 7 : day) - 1) * 86400000;
  return {
    startTime: toString(monday),
    endTime: toString(Date.now() + 86400000),
  };
}

/** 本月1日0点～现在 */
export function getRangeMonth(): TimeFilter {
  const today = new Date();
  const year = today.getFullYear();
  const month = today.getMonth() + 1;

  const firstDay = new Date(`${year}/${month}/01 00:00`);
  return {
    startTime: toString(firstDay.getTime()),
    endTime: toString(Date.now() + 86400000),
  };
}

/** 本年1月1日0点～现在 */
export function getRangeYear(): TimeFilter {
  const today = new Date();
  const year = today.getFullYear();

  const firstDay = new Date(`${year}/01/01 00:00`);
  return {
    startTime: toString(firstDay.getTime()),
    endTime: toString(Date.now() + 86400000),
  };
}

/** 获取指定月份的开始和结束时间 */
export function getMonthTime(year: number, month: number) {
  const start = new Date(year, month - 1, 1);
  const end_mill = new Date(year, month, 1).getTime() - 1;
  const end = new Date(end_mill);
  return {
    startTime: toString(start.getTime()),
    endTime: toString(end.getTime()),
  };
}

/** 去年07月01日0点～现在 */
export function getRangeYearLast(): TimeFilter {
  const today = new Date();
  const year = today.getFullYear();

  const firstDay = new Date(`${year - 1}/07/01 00:00`);
  return {
    startTime: toString(firstDay.getTime()),
    endTime: toString(Date.now() + 86400000),
  };
}

/** 获取最近n天 */
export function getRangeRecent(day = 180): TimeFilter {
  const today = new Date();
  const firstDay = new Date(today.getTime() - day * 86400000);
  firstDay.setHours(0);
  firstDay.setMinutes(0);
  firstDay.setSeconds(0);
  firstDay.setMilliseconds(0);

  return {
    startTime: toString(firstDay.getTime()),
    endTime: toString(today.getTime() + 86400000),
  };
}

/** 获取从A-B时间，共有多少天时秒 */
export function getTimeGap(big: number, small: number) {
  const gap = big - small;

  const hour = ~~(gap / 1000 / 60 / 60) % 24;
  const _hour = hour < 10 ? `0${hour}` : hour.toString();

  const min = ~~(gap / 1000 / 60) % 60;
  const _min = min < 10 ? `0${min}` : min.toString();

  const sec = ~~(gap / 1000) % 60;
  const _sec = sec < 10 ? `0${sec}` : sec.toString();

  const day = ~~(gap / 1000 / 60 / 60 / 24);

  return `${day}天 ${_hour}:${_min}:${_sec}`;
}

/** 获取标准时间字符串 */
export function getTime(mills: any) {
  const date = new Date(toNumber(mills));

  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const monthStr = month < 10 ? `0${month}` : month;
  const day = date.getDate();
  const dayStr = day < 10 ? `0${day}` : day;

  return {
    mills,
    text: `${year}/${monthStr}/${dayStr}`,
    text_full: `${year}/${monthStr}/${dayStr} ${date.toLocaleString("zh", {
      hour12: false,
      hour: "2-digit",
      minute: "2-digit",
      second: "2-digit",
    })}`,
  };
}

/** 指定时长的 Promise */
export function promiseSleep(seconds: number = 5000) {
  return new Promise((resolve) => {
    setTimeout(() => resolve(true), seconds);
  });
}

/** 获取过去时间，距离现在时间的描述 */
export function getTimeAgo(millsPast: any) {
  const now = new Date().getTime();
  const past = new Date(toNumber(millsPast)).getTime();
  const diffInSeconds = Math.floor((now - past) / 1000);

  const secondsInMinute = 60;
  const secondsInHour = 60 * secondsInMinute;
  const secondsInDay = 24 * secondsInHour;
  const secondsInWeek = 7 * secondsInDay;
  const secondsInMonth = 30 * secondsInDay;
  const secondsInYear = 365 * secondsInDay;

  if (diffInSeconds < secondsInMinute) {
    return `${diffInSeconds} 秒前`;
  } else if (diffInSeconds < secondsInHour) {
    const minutes = Math.floor(diffInSeconds / secondsInMinute);
    return `${minutes} 分钟前`;
  } else if (diffInSeconds < secondsInDay) {
    const hours = Math.floor(diffInSeconds / secondsInHour);
    return `${hours} 小时前`;
  } else if (diffInSeconds < secondsInWeek) {
    const days = Math.floor(diffInSeconds / secondsInDay);
    return `${days} 天前`;
  } else if (diffInSeconds < secondsInMonth) {
    const weeks = Math.floor(diffInSeconds / secondsInWeek);
    return `${weeks} 周前`;
  } else if (diffInSeconds < secondsInYear) {
    const months = Math.floor(diffInSeconds / secondsInMonth);
    return `${months} 个月前`;
  } else {
    const years = Math.floor(diffInSeconds / secondsInYear);
    return `${years} 年前`;
  }
}
